/**
 * Copyright (c) 2012, David Varbel
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.epistrata.neutraltheoryapplet;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

import com.epistrata.experiment.Experiment;
import com.epistrata.experiment.Model;
import com.epistrata.experiment.ModelFactory;
import com.epistrata.experiment.Run;
import com.epistrata.experiment.View;
import com.epistrata.neutraltheory.NeutralTheoryModel;

public class ControlPanel extends JPanel {
	private static final long serialVersionUID = -311048543602306602L;
	
	private boolean running = false;
	private boolean paused = false;
	private boolean pcaView = false;
	private JButton startRunButton;
	private JButton pauseRunButton;
	private JButton pcaViewButton;
	private PopulationPanelView populationPanel;
	private StatisticsPanelView statisticsPanel;
	private ExecutorService executor;
	private URL codeBase;


	private static ControlPanel singleton;

	public static ControlPanel getInstance() {
		if (singleton == null) {
			singleton = new ControlPanel();
		}
		
		return singleton;
	}

	private ControlPanel() {
		populationPanel = new PopulationPanelView();
		statisticsPanel = new StatisticsPanelView();

		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

	    startRunButton = new JButton("Start New Run");
	    startRunButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				ControlPanel controlPanel = ControlPanel.getInstance();
				controlPanel.startRun();
			}
	    	
	    });
	    add(startRunButton);
	    
	    pauseRunButton = new JButton("Pause");
	    pauseRunButton.addActionListener( new ActionListener() {
	    	
			@Override
			public void actionPerformed(ActionEvent event) {
				ControlPanel controlPanel = ControlPanel.getInstance();
				controlPanel.togglePaused();
			}
	    	
	    });
	    add(pauseRunButton);

	    pcaViewButton = new JButton("Turn PCA View On");
	    pcaViewButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				ControlPanel controlPanel = ControlPanel.getInstance();
				controlPanel.togglePca();
			}
	    	
	    });
	    add(pcaViewButton);
		
	}
	
	
	private void togglePca() {
		if (pcaView) {
			turnPcaOff();
		} else {
			turnPcaOn();
		}

	}

	private void turnPcaOn() {
		pcaViewButton.setText("Turn PCA View Off");
		pcaView = true;
		populationPanel.setPcaView(true);
	}
	
	private void turnPcaOff() {
		pcaViewButton.setText("Turn PCA View On");
		pcaView = false;
		populationPanel.setPcaView(false);
	}
	
	private void togglePaused() {
		if (paused) {
			pauseRunButton.setText("Pause");
			paused = false;
			populationPanel.setPause(false);
		} else {
			pauseRunButton.setText("Unpause");
			paused = true;
			populationPanel.setPause(true);
		}
		
	}
	
	private void startRun() {
		if (!running) {
			running = true;

			executor = Executors.newSingleThreadExecutor();
			
			executor.execute( new Runnable() {

				@Override
				public void run() {
					ArrayList<View> customViews = new ArrayList<View>();
					customViews.add(populationPanel);
					customViews.add(statisticsPanel);
					//String config = codeBase.getPath();
					//config = "http://www.epistrata.com/neutralTheoryAppletConfig.xml"; //TODO load from URL
					String config = "C:/My Dropbox/Personal/epistrata.com/development/website/neutralTheoryAppletConfig.xml";
					//String design = codeBase.getPath();
					//design = "http://www.epistrata.com/neutralTheoryAppletDesignMatrix.txt";
					String design = "C:/My Dropbox/Personal/epistrata.com/development/website/neutralTheoryAppletDesignMatrix.txt";
					Experiment experiment = new Experiment(config, design, customViews, 1);
					experiment.runExperiment(new ModelFactory() {
						public Model newInstance(Run run) {
							return new NeutralTheoryModel(run);
						}
					});
					running = false;
				}
				
			});
		}
	}

	public PopulationPanelView getPopulationPanel() {
		return populationPanel;
	}

	public StatisticsPanelView getStatisticsPanel() {
		return statisticsPanel;
	}
	
	public void setCodeBase(URL codeBase) {
		this.codeBase = codeBase;
	}
}
